package vos.vn;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class GoogleMapActivity extends MapActivity  implements LocationListener {
	MapView mapView;
	MapController mapController;
	HelloItemizedOverlay itemizedoverlay ;
	OverlayItem overlayitem;
	List<Overlay> mapOverlays;
	int level = 1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mapView = (MapView) findViewById(R.id.mapview);
        mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
        itemizedoverlay = new HelloItemizedOverlay(drawable);                     
        
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
		String bestProvider = lm.getBestProvider(criteria, true);
		lm.requestLocationUpdates(bestProvider, 0, 0, this);		 
        Location location = lm.getLastKnownLocation(bestProvider);
        
        
        //default location
        double longitude = 0;
    	double latitude =0;    	
    	//current location
        if(location!= null) {
        	longitude = location.getLongitude();
        	latitude = location.getLatitude();
        }        
		GeoPoint point = new GeoPoint((int)latitude, (int)longitude);
        overlayitem = new OverlayItem(point, "Sekai, konichiwa!", "I'm in Japan!");        
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);
        
        //center point
        mapController = mapView.getController();
        mapController.animateTo(point);    
        
        
        //test height level 
        Button button = (Button)findViewById(R.id.button1);        
        OnClickListener clickListener = new OnClickListener() {				
			@Override
			public void onClick(View v) {				
				setHeightLevel(level++);				
			}
		};		
		button.setOnClickListener(clickListener);
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        MapController mc = mapView.getController(); 
        switch (keyCode) 
        {
            case KeyEvent.KEYCODE_DPAD_UP:
                mc.zoomIn();
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                mc.zoomOut();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

	@Override
	public void onLocationChanged(Location location) {
		
		 mapOverlays.clear();		
		 GeoPoint point = new GeoPoint((int)location.getLatitude(), (int)location.getLongitude());
	     overlayitem = new OverlayItem(point, "Sekai, konichiwa!", "I'm in Japan!");        
	     itemizedoverlay.addOverlay(overlayitem);
	     mapOverlays.add(itemizedoverlay);
	     mapController.animateTo(point);		    	    	     
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub		
	}    

	/**
	 * @param level the level of MapView Heigh. It is limited between 1 -- 21
	 */
	public void setHeightLevel(int level) {
		level = (level % 21) + 1;
		Toast.makeText(getApplicationContext(),
				"current height level = "+level , 
				Toast.LENGTH_SHORT).show();
		mapController.setZoom(level);
	}
}